using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;

public class ParticleSystemPoolNodeQueue
{
    public bool IsMainChain { get; private set; }

    public ParticleSystemPoolNode Head { get; private set; }
    
    public ParticleSystemPoolNode Tail { get; private set; }

    // 仅用于调试
    List<ParticleSystemPoolNode> List
    {
        get
        {
            List<ParticleSystemPoolNode> nodes = new List<ParticleSystemPoolNode>();
            ParticleSystemPoolNode current = Head;
            while (current != null)
            {
                nodes.Add(current);
                current = current.GetNextNode(IsMainChain);
            }
            return nodes;
        }
    }

    public int Count { get; private set; }

    public ParticleSystemPoolNodeQueue(bool isMainChain)
    {
        IsMainChain = isMainChain;
        Head = null;
        Tail = null;
        Count = 0;
    }

    // 只能追加在队尾
    public void Enqueue(ParticleSystemPoolNode node)
    {
        if (Head == null)
        {
            Head = node;
            Tail = node;
        }
        else
        {
            if (IsMainChain)
            {
                node.MainChainPre = Tail;
                Tail.MainChainNext = node;
            }
            else
            {
                node.SubChainPre = Tail;
                Tail.SubChainNext = node;
            }
            Tail = node;
        }
        Count++;
    }

    public ParticleSystemPoolNode Peek()
    {
        return Head;
    }

    // 可以从任意位置删除节点
    public void Remove(ParticleSystemPoolNode node)
    {
        if (node == Head)
        {
            Head = node.GetNextNode(IsMainChain);
            if (Head != null)
            {
                if (IsMainChain)
                {
                    Head.MainChainPre = null;
                }
                else
                {
                    Head.SubChainPre = null;
                }
            }
            else
            {
                Tail = null; // 如果只有一个节点
            }
        }
        else if (node == Tail)
        {
            Tail = node.GetPreNode(IsMainChain);
            if (Tail != null)
            {
                if (IsMainChain)
                {
                    Tail.MainChainNext = null;
                }
                else
                {
                    Tail.SubChainNext = null;
                }
            }
            else
            {
                Head = null; // 如果只有一个节点
            }
        }
        else
        {
            ParticleSystemPoolNode preNode = node.GetPreNode(IsMainChain);
            ParticleSystemPoolNode nextNode = node.GetNextNode(IsMainChain);
            Assert.IsTrue(preNode != null && nextNode != null, "Middle nodes must have both previous and next nodes.");

            if (IsMainChain)
            {
                preNode.MainChainNext = nextNode;
                nextNode.MainChainPre = preNode;
            }
            else
            {
                preNode.SubChainNext = nextNode;
                nextNode.SubChainPre = preNode;
            }
        }

        if (IsMainChain)
        {
            node.MainChainPre = null;
            node.MainChainNext = null;
        }
        else
        {
            node.SubChainPre = null;
            node.SubChainNext = null;
        }
        Count--;
    }
}
